有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带水平滚动的java嵌套回收视图

我试图用3层实现嵌套的RecyclerView。第一个RecyclerView(父)视图设置为垂直滚动。第二个RecyclerView(第一个视图的子视图)设置为水平滚动。第三个RecyclerView(第二个视图的子视图)也设置为水平滚动

要可视化:

->RecyclerView具有垂直滚动
--&燃气轮机RecyclerView带水平滚动
---&燃气轮机RecyclerView带水平滚动

现在,问题是我无法使第三个水平循环视图水平滚动。我认为问题在于设备正在优先处理第二个RecyclerView的水平滚动

你能帮我解决这个问题吗

这是第一个垂直滚动布局的代码片段:

<安卓.support.v4.widget.NestedScrollView
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent">

    <安卓.support.v7.widget.RecyclerView
        安卓:layout_width="match_parent"            
        安卓:layout_height="wrap_content"/>

</安卓.support.v4.widget.NestedScrollView>

这是用于第二个具有水平滚动的:

<LinearLayout
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content">

    <安卓.support.v7.widget.RecyclerView
        安卓:layout_width="match_parent"            
        安卓:layout_height="wrap_content"/>

</LinearLayout>

这是最后一个带有水平滚动的:

<TextView
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"/>

我已经尝试用onIntercept...()截取触摸,以在触摸第三个RecyclerView时取消第二个RecyclerView的滚动


共 (1) 个答案

  1. # 1 楼答案

    public class NestedScrollViewHome  extends NestedScrollView {
        @SuppressWarnings("unused")
        private int slop;
        @SuppressWarnings("unused")
        private float mInitialMotionX;
        @SuppressWarnings("unused")
        private float mInitialMotionY;
        public NestedScrollViewHome(Context context) {
            super(context);
            init(context);
        }
        private void init(Context context) {
            ViewConfiguration config = ViewConfiguration.get(context);
            slop = config.getScaledEdgeSlop();
        }
        public NestedScrollViewHome(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context);
        }
        public NestedScrollViewHome(Context context, AttributeSet attrs,
                                  int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init(context);
        }
        private float xDistance, yDistance, lastX, lastY;
        @SuppressWarnings("unused")
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            final float x = ev.getX();
            final float y = ev.getY();
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    xDistance = yDistance = 0f;
                    lastX = ev.getX();
                    lastY = ev.getY();
                    // This is very important line that fixes
                    computeScroll();
                    break;
                case MotionEvent.ACTION_MOVE:
                    final float curX = ev.getX();
                    final float curY = ev.getY();
                    xDistance += Math.abs(curX - lastX);
                    yDistance += Math.abs(curY - lastY);
                    lastX = curX;
                    lastY = curY;
                    if (xDistance > yDistance) {
                        return false;
                    }
            }
            return super.onInterceptTouchEvent(ev);
        }
        public interface OnScrollChangedListener {
            void onScrollChanged(NestedScrollView who, int l, int t, int oldl,
                                 int oldt);
        }
        private OnScrollChangedListener mOnScrollChangedListener;
        public void setOnScrollChangedListener(OnScrollChangedListener listener) {
            mOnScrollChangedListener = listener;
        }
        @Override
        protected void onScrollChanged(int l, int t, int oldl, int oldt) {
            super.onScrollChanged(l, t, oldl, oldt);
            if (mOnScrollChangedListener != null) {
                mOnScrollChangedListener.onScrollChanged(this, l, t, oldl, oldt);
            }
        }
    }